﻿using System;
using System.Runtime.Serialization;
using System.Text;

namespace PasswordProvider.ObjectModel
{
    [DataContract]
    public class CreditCardSite : Site
    {
        public CreditCardSite() { }
        public CreditCardSite(string name, string description)
            : base(name, description)
        {
        }

        #region Public Data Members
        
        [DataMember]
        public EncryptedValue CardNumber
        {
            get 
            {
                if (_cardNumber == null)
                    CardNumber = new EncryptedValue();
                return _cardNumber; 
            }
            private set 
            {
                _cardNumber = value;
                if (_cardNumber != null)
                {
                    _cardNumber.PropertyChanged += delegate { OnPropertyChanged("CardNumber"); };
                }
            }
        } private EncryptedValue _cardNumber;

        [DataMember]
        public EncryptedValue Code
        {
            get
            {
                if (_code == null)
                    Code = new EncryptedValue();
                return _code;
            }
            private set
            {
                _code = value;
                if (_code != null)
                    _code.PropertyChanged += delegate { OnPropertyChanged("Code"); };
            }
        } private EncryptedValue _code; 

        [DataMember]
        public EncryptedValue Pin
        {
            get
            {
                if( _pin == null)
                    Pin = new EncryptedValue();
                return _pin; 
            }
            set
            {
                _pin = value; 
                if(_pin != null)
                    _pin.PropertyChanged += delegate { OnPropertyChanged("Pin"); };
            }
        } private EncryptedValue _pin;

        [DataMember]
        public LogOnSite Website
        {
            get;
            set;
        }
       
        [DataMember]
        public string ExpirationMonth
        {
            get { return _expirationMonth; }
            set { SetPropertyValue(value, ref _expirationMonth, "ExpirationMonth"); }
        } private string _expirationMonth; 
        
        [DataMember]
        public string ExpirationYear
        {
            get { return _expirationYear; }
            set { SetPropertyValue(value, ref _expirationYear, "ExpirationYear"); }
        } private string _expirationYear;

        [DataMember]
        public string IssuingCompany
        {
            get { return _issuingCompany; }
            set { SetPropertyValue(value, ref _issuingCompany, "IssuingCompany"); }
        } private string _issuingCompany;

        [DataMember]
        public string CompanyPhone
        {
            get { return _companyPhone; }
            set { SetPropertyValue(value, ref _companyPhone, "IssuingCompany"); }
        } private string _companyPhone;
        
        [DataMember]
        public string NameOnCard
        {
            get { return _nameOnCard; }
            set { SetPropertyValue(value, ref _nameOnCard, "NameOnCard"); }
        } private string _nameOnCard;


        #endregion

        



        #region Overrides

        public override EncryptedValue PrimaryEncryptedValue
        {
            get { return CardNumber; }
        }

        internal override Site Clone()
        {
            CreditCardSite clone = new CreditCardSite();
            clone.Copy(this);
            return clone;
        }

        internal override void Copy(Site site)
        {
            base.Copy(site);

            CreditCardSite creditCard = (CreditCardSite)site;

            CardNumber = creditCard.CardNumber;
            Code = creditCard.Code;
            Pin = creditCard.Pin;
            ExpirationMonth = creditCard.ExpirationMonth;
            ExpirationYear = creditCard.ExpirationYear;
        }

        internal override string WriteExportString(SecurityManager securityManager, bool decryptAll)
        {
            StringBuilder builder = new StringBuilder(Name);
            
            builder.Append("\nCC Number:");
            builder.Append(decryptAll ? securityManager.GetValueDecrypted(CardNumber, false).UnprotectedString : "************");
            builder.Append("\nCC Code: ");
            builder.Append(decryptAll ? securityManager.GetValueDecrypted(Code, false).UnprotectedString : "***");
            builder.Append("\nCC PIN: ");
            builder.Append(decryptAll ? securityManager.GetValueDecrypted(Pin, false).UnprotectedString : "****");
            builder.Append("\nDescription - ");
            builder.Append(Description);
            builder.Append("\n");

            return builder.ToString();
        }

        #endregion // Internal

        
    }
}
